package org.jsola.hr.service.appro.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsola.approval.vo.ApproModuleContext;
import org.jsola.hr.common.ApproHelper;
import org.jsola.hr.entity.CostBillDO;
import org.jsola.hr.service.ICostBillService;
import org.jsola.hr.service.appro.AbstractApproListener;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static org.jsola.hr.constant.CostBillStatus.*;
import static org.jsola.hr.constant.HrConstants.*;

/**
 * @author wcl
 */
@Component
@Slf4j
public class CostBillApproListener extends AbstractApproListener {

    @Autowired
    ICostBillService costBillService;

    /**
     * 通过审批
     */
    @Override
    @Transactional(value = TX, rollbackFor = Exception.class)
    public ApproModuleContext passAppro(ApproModuleContext approModuleContext) {
        TokenUser tokenUser = approModuleContext.getTokenUser();
        String subjectType = approModuleContext.getSubjectType();
        String subjectId = approModuleContext.getSubjectId();
        if (SUBJECT_TYPE_COST_BILL.equals(subjectType)) {
            CostBillDO costBillDO = costBillService.selectById(Long.parseLong(subjectId));
            costBillDO.setApproStatus(PASS_WAIT_SETTLE.getValue());
            costBillService.updateByIdSelective(costBillDO, tokenUser.getSiteId(), tokenUser.getUserId());
            log.info("费用账单审批通过:账单id:{},流程id:{}", costBillDO.getId(), approModuleContext.getFlowId());
        }
        return approModuleContext;
    }

    /**
     * 驳回
     */
    @Override
    @Transactional(value = TX, rollbackFor = Exception.class)
    public ApproModuleContext rejectAppro(ApproModuleContext approModuleContext) {
        TokenUser tokenUser = approModuleContext.getTokenUser();
        String subjectType = approModuleContext.getSubjectType();
        String subjectId = approModuleContext.getSubjectId();
        if (SUBJECT_TYPE_COST_BILL.equals(subjectType)) {
            CostBillDO costBillDO = costBillService.selectById(Long.parseLong(subjectId));
            costBillDO.setApproStatus(APPRO_FAIL.getValue());
            costBillService.updateByIdSelective(costBillDO, tokenUser.getSiteId(), tokenUser.getUserId());
            log.info("费用账单审批拒绝:账单id:{},流程id:{}", costBillDO.getId(), approModuleContext.getFlowId());
        }
        return approModuleContext;
    }

    /**
     * 撤回
     */
    @Override
    @Transactional(value = TX, rollbackFor = Exception.class)
    public ApproModuleContext revokeAppro(ApproModuleContext approModuleContext) {
        TokenUser tokenUser = approModuleContext.getTokenUser();
        String subjectType = approModuleContext.getSubjectType();
        String subjectId = approModuleContext.getSubjectId();
        if (SUBJECT_TYPE_COST_BILL.equals(subjectType)) {
            CostBillDO costBillDO = costBillService.selectById(Long.parseLong(subjectId));
            costBillDO.setApproStatus(WAIT_SETTLE.getValue());
            costBillService.updateByIdSelective(costBillDO, tokenUser.getSiteId(), tokenUser.getUserId());
            log.info("费用账单审批撤销:账单id:{},流程id:{}", costBillDO.getId(), approModuleContext.getFlowId());
        }
        return approModuleContext;
    }

    @Override
    public String getSubjectType() {
        return SUBJECT_TYPE_COST_BILL;
    }

    @Override
    public String getCode(ApproModuleContext approModuleContext) {
        String subjectType = approModuleContext.getSubjectType();
        String code = "";
        if (SUBJECT_TYPE_COST_BILL.equals(subjectType)) {
            CostBillDO costBillDO = ApproHelper.getFormValueToPojo(approModuleContext, CostBillDO.class);
            if (costBillDO != null) {
                // 账单业务类型
                String subjectType1 = costBillDO.getSubjectType();
                if (SALARY_BILL.equals(subjectType1)) {
                    code = APPROVE_SALARY_BILL;
                } else if (SOC_BILL.equals(subjectType1)) {
                    code = APPROVE_SOC_BILL;
                } else if (PF_BILL.equals(subjectType1)) {
                    code = APPROVE_PF_BILL;
                }
            }
        }
        return code;
    }
}
